/**
 * TabVD is a Virtual Desktop Client/Server designed specifically for connecting
 * from Tablet like devices.
 *
 * Copyright (C) 2011 Subhabrata Ghosh
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * XMLHelper.java
 *
 *  Created on: Jul 11, 2011
 *      Author: subhagho
 */
package net.langene.tabvd.core;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author subhagho
 * 
 */
public class XMLHelper {
	private Document document = null;
	private XPath xpath = null;

	/**
	 * net.langene.tabvd.core.XMLHelper
	 * 
	 * @param filename
	 * @throws Exception
	 */
	public XMLHelper(String filename) throws Exception {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory
				.newInstance();
		domFactory.setNamespaceAware(true);
		DocumentBuilder builder = domFactory.newDocumentBuilder();
		document = builder.parse(filename);
		xpath = XPathFactory.newInstance().newXPath();
	}

	/**
	 * net.langene.tabvd.core.XMLHelper
	 * 
	 * @param file
	 * @throws Exception
	 */
	public XMLHelper(InputStream file) throws Exception {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory
				.newInstance();
		domFactory.setNamespaceAware(true);
		DocumentBuilder builder = domFactory.newDocumentBuilder();
		document = builder.parse(file);
		xpath = XPathFactory.newInstance().newXPath();
	}

	/**
	 * net.langene.tabvd.core.XMLHelper
	 * 
	 * @param expr
	 * @return
	 * @throws Exception
	 */
	public NodeList find(String expr) throws Exception {
		XPathExpression xexpr = xpath.compile(expr);

		Object result = xexpr.evaluate(document, XPathConstants.NODESET);
		return (NodeList) result;
	}

	public NodeList find(Node parent, String expr) throws Exception {
		XPathExpression xexpr = xpath.compile(expr);

		Object result = xexpr.evaluate(parent, XPathConstants.NODESET);
		return (NodeList) result;
	}

	/**
	 * net.langene.tabvd.core.XMLHelper
	 * 
	 * @param expr
	 * @return
	 * @throws Exception
	 */
	public List<String> values(String expr) throws Exception {
		NodeList nvalues = find(expr);
		List<String> values = new ArrayList<String>();
		if (nvalues != null && nvalues.getLength() > 0) {
			for (int ii = 0; ii < nvalues.getLength(); ii++) {
				Element elem = (Element) nvalues.item(ii);
				values.add(elem.getTextContent());
			}
		}
		return values;
	}
}
